#############################################################################
# 需要绘制出沿中心角度的一个分布，同时需要绘制出沿半径的一个分布情况
# 首先获取json文件
# 接着利用那些数据进行画图

'''
one_file_info,_ = get_json_data('../COSSY/annotations/Market2.json',write_to_txt=False)
one_file_boxes_angle = list()          # 统计所有角度
import numpy as np

for img in one_file_info:
    if not os.path.exists(os.path.join('../COSSY/Market2/',img['filename']+'.jpg')):
        print(img['filename'])
'''
from tools.get_data_info import get_json_data
import math
import numpy as np
import copy
# 画图
imgs_boxes = list()
#one_file_info,one_file_boxes = get_json_data('../COSSY/annotations_enhance/Market2_enhance.json',write_to_txt=False)
#one_file_info,one_file_boxes = get_json_data('../COSSY_train_shao/annotations/Market1.json',write_to_txt=False)
#one_file_info,one_file_boxes = get_json_data('../COSSY_train/annotations/Market1.json',write_to_txt=False)
one_file_info,one_file_boxes = get_json_data('../COSSY_train_new_back/annotations_frequencyMU/Market1_frequencyMU.json'
                                            ,write_to_txt=False)
one_file_boxes_angle = list()          # 统计所有角度


for img in one_file_info:
    for box in img['boxes']:
        img_cx = img['width']//2        # 中心点坐标
        img_cy = img['height']//2
        angle = math.atan2((box[1]-img_cy),(box[0]-img_cx))/math.pi*180   # 角度范围为[-pi,pi]，将其变换到0-2pi
        one_file_boxes_angle.append(copy.deepcopy(angle))
# 绘制bbox 中心点和 图像中心点夹角 的柱状图
one_file_boxes_angle = np.array(one_file_boxes_angle)

print(one_file_boxes_angle)
import matplotlib.pyplot as plt

X = np.linspace(-175,175,36)
Y = list()
for i in range(len(X)):
    num = ((one_file_boxes_angle >=(X[i]-5))&(one_file_boxes_angle <=(X[i]+5))).sum()
    Y.append(num)
fig = plt.figure()
plt.bar(X, Y, width=5, color="green")
plt.xlabel("angle between box center-image center and x-axis")
plt.ylabel("box num")
plt.title("box center distribution([-pi,pi]) around image center(angle enhance)")


#plt.savefig("中心点角度分布.jpg")


# 中心点距离分布情况
img_radius = max(1080,1080)//2
seperate_group = 20
single_distance = img_radius//seperate_group
L2_dist = np.array(one_file_boxes)[:,-2]      # 获取L2距离
X = np.linspace(0,img_radius-single_distance,seperate_group)
X_num = np.append(X,img_radius)
Y = list()
for i in range(0,len(X_num)-1):
    num = ((L2_dist >=(X_num[i]))&(L2_dist <=(X_num[i+1]))).sum()
    Y.append(num)
fig = plt.figure()
plt.bar(X, Y, width=single_distance//2, color="green")
plt.xlabel("box center L2_dist from img center")
plt.ylabel("box numbers in corresponding range")
plt.title("Market1 L2dist Radius distribution(after frequency mix up)")

plt.show()
#plt.savefig("中心点角度分布.jpg")

# 按照分布进行采样
